//
//  BsTenantsViewController.m
//  SameWorld
//
//  Created by 李小朋 on 2017/3/29.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "BsTenantsViewController.h"
#import "BsTenantsView.h"
#import "CommonMethod.h"
#import "UIBarButtonItem+Extension.h"
#import "BsTenantsViewModel.h"
@interface BsTenantsViewController ()
@property (nonatomic ,strong) BsTenantsView *tenantsView;

//** 获取验证码的时间*/
@property (assign, nonatomic) NSInteger allDuration;
@property (strong, nonatomic) NSTimer *timer;

// ** 验证码*/
@property (nonatomic ,strong) NSString *verificationCode;
@end

@implementation BsTenantsViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"我要入驻";
    self.edgesForExtendedLayout = UIRectEdgeNone;

    [self setNarBar];
    [self createUI];
}

// 发送验证码
- (void)loadDataSendCode {

    NSString *userPhone = self.tenantsView.userPhone.text;
    //http://118.190.98.64/tctxAPI/reg_user .aspx?phone=手机号
    [BsTenantsViewModel loadSendVerificationCodeNetWorking:userPhone andReturnValue:^(id returnValue) {
        NSLog(@"获取验证码：%@",returnValue);
        self.verificationCode = [NSString stringWithFormat:@"%@",returnValue];
        
    } andReturenError:^(id error) {
        NSLog(@"未成功%@",error);
        [self showAlertWithTitle:@"发送失败！"];
    }];

    
}

// 提交验证码
- (void)loadDataSubmitCode {

    NSString *userPhone = self.tenantsView.userPhone.text;
    NSString *validationCode = self.tenantsView.validationCode.text;
    NSString *urlString = [NSString stringWithFormat:@"%@/Verification_code.aspx",serverUrl];
    NSDictionary *paramDic = @{@"phone":userPhone,@"code":validationCode};
    [NetworkManager requestPOSTWithURLStr:urlString paramDic:paramDic finish:^(id responseObject) {
        NSLog(@"请求成功%@",responseObject);
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提交验证码" message:@"成功" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        
        [alertController addAction:okAction];
        
        [self presentViewController:alertController animated:YES completion:nil];
        
    } enError:^(NSError *error) {
        NSLog(@"请求失败%@",error);
    }];
}

// 提交表单
- (void)loadDataSubmitForm {

//    118.190.98.64/tctxAPI/submit_form.aspx?name=刘谣&phone=18652281519&code=222222&shopname=刘哥的锅子&dz=徐州市鼓楼区黄河路22号2单元222
    NSString *userName = self.tenantsView.userName.text;
    NSString *userPhone = self.tenantsView.userPhone.text;
    NSString *userCode = self.verificationCode;
    NSString *shopName = self.tenantsView.storesName.text;
    NSString *userDZ = [NSString stringWithFormat:@"%@%@",self.tenantsView.selecdCityBtn.titleLabel.text,self.tenantsView.storesAddress.text];
    [BsTenantsViewModel loadSubmitFormNetWorking:userName andPhone:userPhone andCode:userCode andShopname:shopName andDz:userDZ andReturenValue:^(id returnValue) {
        NSLog(@"请求成功%@",returnValue);
        
        [self showAlertWith:returnValue];
    
    } andReturenError:^(id errorCode) {
        NSLog(@"请求失败%@",errorCode);
        
    }];
}


-(void)setNarBar {
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithHexString:@"#ff9900"]];
    //1.3.1 在具有导航栏的viewController中设置
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem  itemWithTitle:@"返回" ImageName:@"fanhui" highImageName:nil target:self action:@selector(navItemClick:)];
    
}

#pragma mark - 返回
- (void)navItemClick:(UIBarButtonItem *)sender {

    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createUI {
    
    self.tenantsView = [[BsTenantsView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.tenantsView];
    
        YXWeakSelf
#pragma mark - 获取验证码
    self.tenantsView.sendCodeBtn = ^{
        
        if ([CommonMethod validatePhone:weakSelf.tenantsView.userPhone.text]) {
            _allDuration = 30;
            weakSelf.tenantsView.sendCodeButton.enabled = NO;
            [weakSelf.tenantsView.sendCodeButton setTitle:[NSString stringWithFormat:@"%@秒后重新获取",@(weakSelf.allDuration)] forState:(UIControlStateNormal)];
        weakSelf.tenantsView.sendCodeButton.backgroundColor = [UIColor lightGrayColor];
            _timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(timeChanged:) userInfo:nil repeats:YES];
            [weakSelf loadDataSendCode];
        }else{
        
            CustomAlertActionView *alertView = [[CustomAlertActionView alloc] initWithTitle:@"请输入11位手机号" message:nil sureBtn:@"确定" cancleBtn:nil];
            [alertView showCostomAlertView];
        }

    };
    
    self.tenantsView.submitBtn = ^{
        NSLog(@"提交");
        if (self.verificationCode == nil) {
            [weakSelf showAlertWithTitle:@"请输入有效信息！"];
            return ;
        }
        
        [weakSelf loadDataSubmitForm];
    };
    
}

- (void)timeChanged:(NSTimer*)timer{
    
    _allDuration--;
    [self.tenantsView.sendCodeButton setTitle:[NSString stringWithFormat:@"%@秒",@(_allDuration)] forState:UIControlStateNormal];
    if (_allDuration ==0) {
        [self.tenantsView.sendCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.tenantsView.sendCodeButton.backgroundColor = [UIColor colorWithHexString:@"#ff9900"];
        self.tenantsView.sendCodeButton.enabled = YES;
        [self.timer invalidate];
        self.timer =nil;
    }
}

@end
